<template>
  <div class="list-table">
    <div v-if="showTitle" class="area-header">
      <span class="area-header-title">{{ $t('项目回款') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item v-if="!this.$route.query.dealId && showTitle" :label="$t('参股基金')" prop="dealId">
        <el-select v-model="queryParams.dealId" filterable clearable :placeholder="$t('请选择')">
          <el-option
            v-for="item in dealNameOptions"
            :key="item.id"
            :label="item.label"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('回款日期')" prop="moneyBackDate">
        <DatePicker v-model="dateMoneyBacks" type="daterange" />
      </el-form-item>
      <el-form-item :label="$t('回款方式')" prop="moneyBackType">
        <el-select v-model="queryParams.moneyBackType" :placeholder="$t('回款方式')" clearable>
          <el-option
            v-for="dict in moneyBackTypeOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('退出状态')" prop="exitStatus">
        <el-select v-model="queryParams.exitStatus" :placeholder="$t('退出状态')" clearable>
          <el-option
            v-for="dict in exitStatusOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button v-if="showTitle" icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row v-if="showTitle" :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['company:moneyback:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t('新增') }}</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" tooltip-effect="darkTable" :data="companyProjectMoneybackList" border>
      <el-table-column fixed="left" :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('参股基金')" align="center" prop="dealNameLable" min-width="150" />
      <el-table-column :label="$t('回款日期')" align="center" prop="moneyBackDate" show-overflow-tooltip width="140">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.moneyBackDate) }}</span>
        </template>
      </el-table-column>
      <!--<el-table-column :label="$t('回款方式')" align="center" prop="moneyBackType" width="140" show-overflow-tooltip :formatter="moneyBackTypeFormat" />-->
      <el-table-column :label="$t('本次退出本金') + $t('金额单位')" align="center" prop="quitCaptial" show-overflow-tooltip width="180">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.quitCaptial, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('本次退出收益') + $t('金额单位')" align="center" prop="quitProfit" show-overflow-tooltip width="180">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.quitProfit, 'w') }}</span>
        </template>
      </el-table-column>
      <!--
      <el-table-column :label="$t('分红') + $t('金额单位')" align="center" prop="shareBonus" show-overflow-tooltip width="110">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.shareBonus, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('其他') + $t('金额单位')" align="center" prop="other" show-overflow-tooltip width="110">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.other, 'w') }}</span>
        </template>
      </el-table-column>
      -->
      <el-table-column :label="$t('本次退出总额') + $t('金额单位')" align="center" prop="quitTotalAmount" show-overflow-tooltip width="180">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.quitTotalAmount, 'w') }}</span>
        </template>
      </el-table-column>
      <!--
      <el-table-column :label="$t('本次退出比例(%)')" align="center" prop="quitProportion" show-overflow-tooltip width="180">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.quitProportion) }}</span>
        </template>
      </el-table-column>
      -->
      <el-table-column :label="$t('退出状态')" align="center" prop="exitStatus" :formatter="exitStatusFormat" show-overflow-tooltip width="140" />
      <el-table-column v-if="showTitle" fixed="right" :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['company:moneyback:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-hasPermi="['company:moneyback:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改项目回款对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <companyProjectMoneybackForm ref="companyProjectMoneybackForm" />
    </el-dialog>
  </div>
</template>

<script>
import { listCompanyProjectMoneyback, delCompanyProjectMoneyback, exportCompanyProjectMoneyback } from '@/api/company/moneyback/companyProjectMoneyback'
import companyProjectMoneybackForm from './companyProjectMoneybackForm.vue'
import { lisDealName } from '@/api/bf/investGroup/bfInvestGroup'
export default {
    components: {
        companyProjectMoneybackForm
    },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          moneyBackTypeOptions: [],
          exitStatusOptions: [],
          // 项目回款表格数据
          companyProjectMoneybackList: [],
          dateMoneyBacks: [],
          dealNameOptions: [],
          dealQueryParams: {
            indirectDealId: this.companyId || this.$route.query.companyId,
            source: 'BF_PORTFOLIO'
          },
          // 弹出层标题
          title: '项目回款',
          // 是否显示弹出层
          formOpen: false,
          // 查询参数
          queryParams: {
              companyId: this.$route.query.companyId,
              // dealId: this.$route.query.dealId,
              moneyBackDate: undefined,
              moneyBackType: undefined,
              exitStatus: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {},
          showTitle: true
      }
  },
  created() {
      this.getDicts('return_money_mode').then(response => {
        this.moneyBackTypeOptions = response.data
      })
      this.getDicts('quit_status').then(response => {
        this.exitStatusOptions = response.data
      })
      // 获取所有关联项目
      lisDealName(this.dealQueryParams).then(response => {
        this.dealNameOptions = response.rows
      })
      this.getList()
  },
  methods: {
    /** 查询项目回款列表 */
    getList() {
        this.loading = true
        if (this.dateMoneyBacks != null && this.dateMoneyBacks.length > 1) {
          this.queryParams.startDate = this.dateMoneyBacks[0]
          this.queryParams.endDate = this.dateMoneyBacks[1]
        } else {
          this.queryParams.startDate = undefined
          this.queryParams.endDate = undefined
        }
        listCompanyProjectMoneyback(this.queryParams).then(response => {
            this.companyProjectMoneybackList = response.rows
            this.total = response.total
            this.loading = false
        })
    },

    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            companyId: this.$route.query.companyId,
            dealId: this.$route.query.id,
            moneyBackDate: undefined,
            moneyBackType: undefined,
            quitCaptial: undefined,
            quitProfit: undefined,
            shareBonus: undefined,
            other: undefined,
            quitTotalAmount: undefined,
            quitProportion: undefined,
            exitStatus: undefined,
            remark: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined
        }
        this.resetForm('form')
    },
    moneyBackTypeFormat(row) {
      return this.selectDictLabel(this.moneyBackTypeOptions, row.moneyBackType)
    },
    exitStatusFormat(row) {
      return this.selectDictLabel(this.exitStatusOptions, row.exitStatus)
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.dateMoneyBacks = []
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.companyProjectMoneybackForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.companyProjectMoneybackForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delCompanyProjectMoneyback(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
        const queryParams = this.queryParams
        this.$confirm(this.$t('是否确认导出数据项?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return exportCompanyProjectMoneyback(queryParams)
        }).then(response => {
            this.download(response.msg)
        }).catch(function() {
        })
    }
  }
}
</script>
